package analyse;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import data.Constant;
import data.LoadData;
import data.TimeStock;

public class IndustrySort {
	private HashMap<String, List<TimeStock>> industryStock ; 
	private static List<IndustryItem> rateSort =  new ArrayList<IndustryItem>(); ; 
	private LinkedHashSet<String> result ;
	public IndustrySort(){
		System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
		
		industryStock = new HashMap<String, List<TimeStock>>(); 
		Collection<TimeStock> stocklist = LoadData.getStockList().values();
		for (TimeStock ts : stocklist) {
			if(ts.getIndustry().trim().equals(""))
				continue;
			
			if(industryStock.get(ts.getIndustry())==null){
				List<TimeStock> tsList = new ArrayList<TimeStock>();
				tsList.add(ts);
				industryStock.put(ts.getIndustry(), tsList);
			}else{
				List<TimeStock> tsList = industryStock.get(ts.getIndustry());
				tsList.add(ts);
				industryStock.put(ts.getIndustry(), tsList);
			}
		}
	}
	

	public void sort(){
		rateSort.clear();
		
		Set<String> indsy = industryStock.keySet();
		
		for (String isy : indsy) {
			List<TimeStock> tlist = industryStock.get(isy);
			
			if(tlist.size()<8){
				continue;
			}
			
			BigDecimal ratePercent = Constant.ZERO;
			for (TimeStock ts : tlist) {
				if(ts.getQty()==0){
					continue;
				}else{
					ratePercent = ratePercent.add(ts.getRatePercent());
				}
			}
			
			Collections.sort(tlist,new Comparator<TimeStock>() {
				@Override
				public int compare(TimeStock o1, TimeStock o2) {
					// TODO Auto-generated method stub
					return o2.getRatePercent().compareTo(o1.getRatePercent());
				}
			});
			
			IndustryItem ini = new IndustryItem();
			ini.setIndustry(isy);
			ini.setAverPercent(ratePercent.divide(new BigDecimal(tlist.size()),2,BigDecimal.ROUND_HALF_UP));
			String[] codes = new String[3];
			for (int i = 0; i < 3; i++) {
				TimeStock ts1 = tlist.get(i);
//				ts1.setHead(true);
				codes[i] = ts1.getCode();
				/*if(ts1.getBeginPrice().subtract(ts1.getPreDayPrice()).multiply(Constant.ONEHUNDRED).divide(ts1.getPreDayPrice(),2,BigDecimal.ROUND_HALF_UP).compareTo(Constant.FOUR)<0){
					if(ts1.getLastStock()!=null&&ts1.getLastStock().getRatePercent().compareTo(Constant.BIGBAT)>0){
						continue;
					}
					for (int j = i+1; j < tlist.size(); j++) {
						TimeStock ts2 = tlist.get(j);
						ts2.setHead(true);
						if(ts2.getBeginPrice().subtract(ts2.getPreDayPrice()).multiply(Constant.ONEHUNDRED).divide(ts2.getPreDayPrice(),2,BigDecimal.ROUND_HALF_UP).compareTo(Constant.FOUR)<0){
							if(ts2.getLastStock()!=null&&ts1.getLastStock().getRatePercent().compareTo(Constant.BIGBAT)>0){
								continue;
							}
							String[] codes = {ts1.getCode(),ts2.getCode()};
							ini.setCode(codes);
							break;
						}
					}
					break;
				}*/
			}
			ini.setCode(codes);
			rateSort.add(ini);
		}
		Collections.sort(rateSort, new Comparator<IndustryItem>(){
			@Override
			public int compare(IndustryItem o1, IndustryItem o2) {
				return o2.getAverPercent().compareTo(o1.getAverPercent());
			}
		});
		result =  new LinkedHashSet<String>();
		int index = 0;
		rateSort = rateSort.subList(0, 10);
		for (IndustryItem it : rateSort) {
			result.add(it.getCode()[0]);
			result.add(it.getCode()[1]);
			result.add(it.getCode()[2]);
			index++;
			if(index<11){
				LoadData.getTimeStock(it.getCode()[0]).setHead(true);
				LoadData.getTimeStock(it.getCode()[1]).setHead(true);
				LoadData.getTimeStock(it.getCode()[2]).setHead(true);
			}
		}
	}
	
	public LinkedHashSet<String> getRateSort() {
		return result;
	}

	public static List<IndustryItem> getRateSort2(){
		return rateSort;
	}
}
